package cn.amoqi.springbootrocketmq.v2.config;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消息队列配置
 */

@Configuration
public class MqConfiguration {

    private final MqProperties mqProperties;


    public MqConfiguration(MqProperties mqProperties) {
        this.mqProperties = mqProperties;
    }

    //阿里云使用
/*    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public Producer proCreateProducer() {
        Properties properties = new Properties();
        // 您在控制台创建的 Consumer ID
        properties.put(PropertyKeyConst.GROUP_ID, mqProperties.getProducerId());
        // AccessKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, mqProperties.getAppKey());
        // SecretKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, mqProperties.getAppSecret());
        // 设置 TCP 接入域名，进入 MQ 控制台的消费者管理页面，在左侧操作栏单击获取接入点获取
        // 此处以公共云生产环境为例
        properties.put(PropertyKeyConst.NAMESRV_ADDR,
                mqProperties.getEndpoint());
        // 集群订阅方式 (默认)
        // properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
        // 广播订阅方式
        // properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);

        return ONSFactory.createProducer(properties);
    }*/


    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public DefaultMQProducer devCreateProducer() {
        DefaultMQProducer producer = new DefaultMQProducer(mqProperties.getProducerId());
        producer.setVipChannelEnabled(false);
        producer.setNamesrvAddr(mqProperties.getUrl());
        producer.setDefaultTopicQueueNums(4);
        return producer;
    }

}
